<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="./vue.js"></script>
    <script src="./vue-router.js"></script>
  </head>
  <body>
    <div id="app">
      <router-link to="/books"> books page</router-link>
      <router-link to="/news"> news page</router-link>
      <router-link to="/user/100"> users page</router-link>
      <hr />

      <a href="#/books">books a</a>
      <a href="#/news">news a</a>
      <hr />

      <router-view></router-view>
    </div>

    <script>
      // 组件
      const Books = {
        template: "<h1>my books page</h1>",
      };

      const News = {
        template: "<h1>my news page</h1>",
      };

      const User = {
        template: "<h1>User-----{{ $route.params.id }}</h1>",
      };

      const NotFound = {
        template: "<h2>页面飞走了 404</h2>",
      };

      //路由挂载组件
      const myRouter = new VueRouter({
        mode: "history", // history 去掉#号 默认哈希

        routes: [
          { path: "/books", component: Books },
          { path: "/news", component: News },
          { path: "/user/:id", component: User },
          { path: "/", redirect: "/news" },
          { path: "*", component: NotFound },
        ],
      });

      var vm = new Vue({
        el: "#app",
        data: {},
        router: myRouter, //挂载路由
      });
    </script>
  </body>
</html>
